package com.asa.hutils;

public class Hanglieshi {

	
	/**
	 * 计算一个二阶行列式的值
	 */
	public static double erjie2value(double a[][]) {
		
		if (a.length!=2||a[0].length!=2) {
			throw new RuntimeException("长度不对");
		}
		System.out.println(a[0][0] +" x "+ a[1][1] +" - " + a[0][1] +" x "+a[1][0]);
		return a[0][0]*a[1][1] - a[0][1]*a[1][0];
	}
	
	/**
	 * 由一个行列式，输入一个i和j来确定它的代数余子式
	 * @param a
	 * @param ai
	 * @param aj
	 * @return
	 */
	public static double[][] daishuyuzixiang(double a[][],int ai,int aj){
		
		double[][] a_ = new double[a.length-1][a.length-1];
		
		for (int i = 0; i < a.length; i++) {
			if (i==ai) {
				continue;
			}
			for (int j = 0; j < a.length; j++) {
				
				if (j==aj) {
					continue;
				}
				
				if (i>ai && j>aj) {
					a_[i-1][j-1] = a[i][j];
				}else if (i>ai && j<aj) {
					a_[i-1][j] = a[i][j];
				}else if (i<ai && j>aj) {
					a_[i][j-1] = a[i][j];
				}else {
					a_[i][j] = a[i][j];
				}
				
				
			}
		}
		return a_;
	}
	
	/**
	 * 求矩阵的伴随矩阵
	 * @param a
	 * @return
	 */
	public static double[][] bangsuijuzheng(double a[][]) {
		
		double[][] result_a = new double[a.length][];
		
		
		for (int i = 0; i < a.length; i++) {
			result_a[i] = new double[a[i].length];

			for (int j = 0; j < a[i].length; j++) {
				double[][] daishuyuzixiang = daishuyuzixiang(a, i, j);
				
				if (daishuyuzixiang.length==2) {
					result_a[i][j] = Math.pow(-1,i + j + 2)*erjie2value(daishuyuzixiang);

				}else {
					result_a[i][j] = value_one(daishuyuzixiang);

				}
				
			}
		}
		
		
		return MathHUtils.zhuanzhi(result_a);//我其实是不知道为什么要转置的

		
	}
	
	
	/**
	 * 伴随矩阵求逆
	 * @param a
	 */
	public static double[][] ni(double a[][]) {
		
		double sum = value_one(a);//矩阵转换成行列式，行列式的值
		
		double[][] bangsuijuzheng = bangsuijuzheng(a);//求解伴随矩阵
		
		double[][] result = new double[a.length][];
		
		for (int i = 0; i < bangsuijuzheng.length; i++) {
			result[i] = new double[bangsuijuzheng[i].length];
			for (int j = 0; j < bangsuijuzheng[i].length; j++) {
				result[i][j] = bangsuijuzheng[i][j]/sum;
			}
		}
		return result;
	}
	
	
	
	
	
	/**
	 * 计算行列式的值
	 */
	public static double[] value(double a[][]) {
		

		
		
		double[] a0 = new double[a.length];
		
		for (int i = 0; i < a[0].length; i++) {
			
			if (i%2==0) {
				a0[i] = a[0][i];
			}else {
				a0[i] = -a[0][i];
			}
//			System.out.println(a0[i]);
		}
		
		double[][][] asa = new double[a.length][][];
		
		for (int i = 0; i < asa.length; i++) {
			asa[i] = daishuyuzixiang(a,0,i);
			for (int j = 0; j < asa[i].length; j++) {
				for (int k = 0; k < asa[i][j].length; k++) {
					System.out.print(asa[i][j][k] + "   ");
				}
				System.out.println();
			}
			System.out.println("==============================");
		}
		
		double[] result = new double[a.length];
		
		if(asa[0].length==2) {
			
			for (int i = 0; i < asa.length; i++) {
				double erjie2value = erjie2value(asa[i]);
				System.out.println(erjie2value);
				result[i] = a0[i] * erjie2value;
			}
			
			
		}else if(asa[0].length==1) {
			result[0] = asa[0][0][0];
			
		}else if(asa[0].length<1){
			throw new RuntimeException("长度不对");
		}else {
			
			for (int i = 0; i < result.length; i++) {
				double[] value = value(asa[i]);
				double sum = 0;
				for (int j = 0; j < value.length; j++) {
					sum += value[j];
				}
				
				result[i] = a0[i] * sum;
				
				System.out.println(sum);
			}

			
		}
		
		
		
		return result;
	}
	
	public static double value_one(double[][] a_) {
		double[] value = value(a_);
		double sum = 0;
		for (int j = 0; j < value.length; j++) {
			sum += value[j];
		}
		return sum;
	}
	
	
	public static void main(String[] args) {
		
//		double[][] a_ = {{2,2,3},{1,-1,0},{-1,2,1}};
		double[][] a_ = {{0,1},{1,-1}};
		
		
		double[][] daishuyuzixiang = daishuyuzixiang(a_, 1, 0);
		
		for (int i = 0; i < daishuyuzixiang.length; i++) {
			for (int j = 0; j < daishuyuzixiang[i].length; j++) {
				System.out.print(daishuyuzixiang[i][j]+"  ");
			}
			System.out.println();
		}
		
		
		
//		double sum = value_one(a_);
		System.out.println("--------最终值-----------");
//		System.out.println(sum);
		
		
		
		System.out.println(Ni.hangleishivalue(a_));

		
		
		
		double[][] bangsuijuzheng = bangsuijuzheng(a_);
		
		
		System.out.println("=====================================");
		for (int i = 0; i < bangsuijuzheng.length; i++) {
			for (int j = 0; j < bangsuijuzheng[i].length; j++) {
				System.out.print(bangsuijuzheng[i][j]+"  ");
			}
			System.out.println();
		}
		
		
		System.out.println("=====================================");

		double[][] ni = ni(a_);
		
		for (int i = 0; i < ni.length; i++) {
			for (int j = 0; j < ni[i].length; j++) {
				System.out.print(ni[i][j]+"  ");
			}
			System.out.println();
		}
		
		
		
		
		
	}

	
	
	
	
	
}
